import { Navigate } from "react-router";
import { useLocation } from "react-router";

import { ReactNode, FC } from "react";

// 导入ReactNode类型
interface Iprops {
  children?: ReactNode
}

// 定义一个函数Private是FC类型 指令泛型为Iprops
const Private: FC<Iprops> = (props) => {
  var token = sessionStorage.getItem('token')
  //  通过useLocation创建一个location对象 包含path属性
  const location = useLocation()
  if (token) {
    return <>{props.children}</>
  } else {
    // 跳转首页，添加redirect查询参数
    // Navigate组件来源react-router-dom 做重定向和跳转用
    return <Navigate to={'/?redirect=' + location.pathname}></Navigate> ///?redirect=
  }
}
export default Private;